﻿using AngleSharp.Dom;
//using SharpSoft.Documentation.Html.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharpSoft.Documentation.Html.CSS
{
    /// <summary>
    /// 属性选择器
    /// </summary>
    public struct AttributeRule : IRule
    {
        /// <param name="name">属性名称</param>
        /// <param name="value">匹配的值</param>
        /// <param name="type"></param>
        public AttributeRule(string name, string value = null, AttributeMatchType type = AttributeMatchType.Equal)
        {
            _Name = name;
            _Value = value;
            _MatchType = type;
            this._RelationType = CssSelectorRuleRelationType.And;
        }

        private string _Name;
        /// <summary>
        /// 属性名称
        /// </summary>
        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        private string _Value;
        /// <summary>
        /// 属性值，为null表示不匹配属性的值，只匹配属性是否存在。此时MatchType设置无效。
        /// </summary>
        public string Value
        {
            get { return _Value; }
            set { _Value = value; }
        }

        private AttributeMatchType _MatchType;
        /// <summary>
        /// 属性值匹配模式
        /// </summary>
        public AttributeMatchType MatchType
        {
            get { return _MatchType; }
            set { _MatchType = value; }
        }

        private CssSelectorRuleRelationType _RelationType;
        /// <summary>
        /// 获取或设置当前规则与下一个规则的关系，对于选择器中的最后一个规则，此设置无效
        /// </summary>
        public CssSelectorRuleRelationType RelationType
        {
            get { return _RelationType; }
            set { _RelationType = value; }
        }

        public override string ToString()
        {
            return string.Format("属性规则：属性名-{0}，匹配类型-{1}，值-'{2}'", Name, MatchType, Value);
        }

        // public NodeFilter ToNodeFilter()
        //{
        //    if (MatchType == AttributeMatchType.Equal && Value == null)
        //    {
        //        AngleSharp.Dom.NodeFilter
        //        return new HasAttributeFilter(Name);
        //    }
        //    string pattern = "";
        //    switch (MatchType)
        //    {
        //        case AttributeMatchType.Equal:
        //            pattern = "^" + Value + "$";
        //            break;
        //        case AttributeMatchType.Item:
        //            pattern = string.Format("^{0} | {0}$| {0} |^{0}$", Value);
        //            break;
        //        case AttributeMatchType.StartWith:
        //            pattern = "^" + Value + "*";
        //            break;
        //        case AttributeMatchType.EndWith:
        //            pattern = Value + "$";
        //            break;
        //        case AttributeMatchType.Contains:
        //            pattern = "*" + Value + "*";
        //            break;
        //        case AttributeMatchType.Specific:
        //            pattern = string.Format("^{0}$|^{0}-", Value);
        //            break;
        //        default:
        //            break;
        //    }

        //    return new AttributeRegexFilter(Name, pattern, false);//属性值匹配需考虑大小写
        //}
    }
    /// <summary>
    /// 属性选择器的匹配模式
    /// </summary>
    public enum AttributeMatchType
    {
        /// <summary>
        /// 完全匹配，=
        /// </summary>
        Equal,
        /// <summary>
        /// 匹配以空格分割的词列表中的一向，如使用[class~="abc"]匹配class="abc def"，~=
        /// </summary>
        Item,
        /// <summary>
        /// 子字符串开头匹配,^=
        /// </summary>
        StartWith,
        /// <summary>
        /// 子字符串结尾匹配,$=
        /// </summary>
        EndWith,
        /// <summary>
        /// 子字符串包含匹配,*=
        /// </summary>
        Contains,
        /// <summary>
        /// 特定属性匹配，如[a|="b"]将只匹配："b"、"b-"、"b-a"、"b-g123"。不会匹配如："ba"、"b.a"、"b123"
        /// </summary>
        Specific,
    }
}
